import {ajax} from '../../../utils/httpRequest.js'
import {URI_SUBCATEGORY} from "../../../api.js";

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showtype: 'zqlq',
    cat_id: '',//一级 id
    reqPage: 1,
    tabName: '',//每次切换要传 name
    goods_list: [],
    icon: '',
    tabs: [],
    sw:true,
    show_loading:false,
    text:'正在加载····',
    scrollTop:0,
    showGoTop:false,
    show_noData:false,
    winHeight: "",//窗口高度
    winWidth: '',//
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //console.log(options);
    wx.setNavigationBarTitle({
      title: options.title
    });
    this.setData({
      currentTab: options.currentIndex,
      cat_id: options.cat_id,
      tabName: options.name,
    });
    this.getGoodsList();
    this.switchTab();
    //console.log('category onLoad', options)
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
            clientWidth = res.windowWidth,
            rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 80;
        that.setData({
          winHeight: calc,
          winWidth: clientWidth
        });
      }
    });
  },

  // 滚动切换标签样式
  switchTab: function (e) {
    //var current='';
    if(e.detail.current){
      var current= e.detail.current;
    }
    console.log(current);
    this.setData({
      currentTab: e.detail.current,
      tabName: this.data.tabs[current].name,
      reqPage:1
    });
    this.checkCor();
    this.getGoodsList();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
   // console.log(e.target.dataset.name);
    wx.pageScrollTo({
      scrollTop: 0
    })
    var cur = e.target.dataset.current;
    this.setData({
      tabName:e.target.dataset.name,
      reqPage:1
    });
     // console.log(cur);
    if (this.data.currentTab == cur) {
      return false;
    }
    else {
      this.setData({
        currentTab: cur,
      })
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。

  checkCor: function () {
    console.log('checkCor')
    if (this.data.currentTab > 3) {
      this.setData({
        scrollLeft: this.data.currentTab*100
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  watchNavScroll(e){
    console.log(e.detail.scrollLeft);
  },
  getGoodsList: function () {
    var that = this;
    ajax.get(URI_SUBCATEGORY, {
      goodCateId: that.data.cat_id,
      name: that.data.tabName,
      page: that.data.reqPage,
      accessToken: wx.getStorageSync('accessToken')
    }, {}, (res) => {
      //console.log(res.data.data);
      that.setData({
        goods_list: res.data.data.commodityList,
        icon: res.data.data.ico,
        tabs: res.data.data.goodCateTwoList,
        show_loading:false,
        text:'正在加载····'
      });
     //console.log(res.data.data.goodCateTwoList);
      this.watchPageData();
      console.log(that.data.goods_list);
    })
  },



  getMoreGoodsList:function(){
    var that = this;
    ajax.get(URI_SUBCATEGORY, {
      goodCateId: that.data.cat_id,
      name: that.data.tabName,
      page: that.data.reqPage,
      accessToken: wx.getStorageSync('accessToken')
    }, {}, (res) => {
      let old_goods_list= that.data.goods_list;
      let resData = res.data.data.commodityList;
      for(var i=0;i<resData.length;i++){
        old_goods_list.push(resData[i])
      }
      that.setData({
        goods_list: old_goods_list,
        sw:true,
        show_loading:false
      });
      if(resData.length<20){
        that.setData({
          sw:false,
          text:'---暂无数据---'
        })
        setTimeout(()=>{
          that.setData({
            show_loading:false
          });
        },1000)
      }
      console.log(that.data.goods_list);
    })
  },

  //判断有没数据；
  watchPageData(){
    if(this.data.goods_list && this.data.goods_list.length>0){
      this.setData({
        show_noData:false
      })
    } else {
      this.setData({
        show_noData:true
      })
    }
  },

  goTop: function () {
    this.setData({
      scrollTop:0
    });
    // if (wx.pageScrollTo) {
    //   wx.pageScrollTo({
    //     scrollTop: 0
    //   })
    // } else {
    //   wx.showModal({
    //     title: '提示',
    //     content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
    //     confirmColor: '#ff474f'
    //   })
    // }
  },
  watchScroll(e){
    var scroll = e.detail.scrollTop;
    if(scroll>500){
      this.setData({showGoTop:true})
    }else{
      this.setData({showGoTop:false})
    }
  },
  footerTap: app.footerTap,
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onBottom:function () {
    var that = this;
    that.setData({
      show_loading:true
    });
    if(this.data.sw){
      this.setData({
        reqPage:this.data.reqPage+1
      })
      // console.log(this.data.pageReq);
      this.getMoreGoodsList();
    }
  },

  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})